package cn.droidlover.xdroidmvp.view;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import cn.droidlover.xdroidmvp.R;
import cn.droidlover.xdroidmvp.util.NetworkUtil;
import cn.droidlover.xdroidmvp.util.ScreenUtils;


public class NotRecordView extends FrameLayout {
    public static final int type_not_data = 1;

    public static final int type_not_net = 2;
    public static final int type_not_message = 3;
    public static final int type_not_search = 4;
    public static final int type_not_shopping_cart = 5;
    private int currentType = 1;
    private TextView tvNotRecordTip;
    private AppCompatImageView ivNotRecord;
    private String tip;
    private Context mContext;

    public NotRecordView(@NonNull Context context) {
        this(context,null);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public NotRecordView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public NotRecordView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public NotRecordView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        initView();
    }

    private void initView() {
        LinearLayout view = createView();
        this.addView(view);
        tvNotRecordTip = view.findViewWithTag(2);
        ivNotRecord = view.findViewWithTag(1);
    }

    private LinearLayout createView(){
        LinearLayout layout = new LinearLayout(mContext);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setGravity(Gravity.CENTER);

        AppCompatImageView imageView = new AppCompatImageView(mContext);
        imageView.setTag(1);
        layout.addView(imageView, ScreenUtils.dp2px(mContext,150f),ScreenUtils.dp2px(mContext,150f));

        AppCompatTextView textView = new AppCompatTextView(mContext);
        textView.setTag(2);
        textView.setTextColor(Color.parseColor("#969696"));
        textView.setTextSize(16f);
        textView.setPadding(0,8,0,8);
        layout.addView(textView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        return layout;
    }

    public void setType(int type) {
        this.currentType = type;
        setData(currentType);
    }

    public void setNotRecordTip(String tip) {
        this.tip = tip;
        tvNotRecordTip.setText(tip);
    }

    private void setData(int type) {
        switch (type) {
            case 1:
                ivNotRecord.setImageResource(R.mipmap.ic_not_record_data);
                tvNotRecordTip.setText("当前没有数据");
                break;
            case 2:
                ivNotRecord.setImageResource(R.mipmap.ic_not_record_data);
                tvNotRecordTip.setText("请检查您的网络设置或重新加载");
                break;
            case 3:
                ivNotRecord.setImageResource(R.mipmap.ic_not_record_data);
                tvNotRecordTip.setText("这里一粒芝麻都没有，空虚寂寞冷");
                break;
            case 4:
                ivNotRecord.setImageResource(R.mipmap.ic_not_record_data);
                tvNotRecordTip.setText("这里一粒芝麻都没有，空虚寂寞冷");
                break;
            case 5:
                ivNotRecord.setImageResource(R.mipmap.ic_not_record_data);
                tvNotRecordTip.setText("这里一粒芝麻都没有，空虚寂寞冷");
                break;
        }
    }

    public void setImageView(int imageId) {
        ivNotRecord.setImageResource(imageId);
    }

    public void setTextContent(String tip) {
        tvNotRecordTip.setText(tip);
    }

    public void showView() {
        setVisibility(VISIBLE);
        if (NetworkUtil.isConnected(getContext())) {
            setType(currentType);
        } else {
            setData(NotRecordView.type_not_net);
        }
    }

    public void hideView() {
        setVisibility(GONE);
    }
}
